package com.ty.zls.components;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.lxj.easyadapter.CommonAdapter;
import com.lxj.easyadapter.MultiItemTypeAdapter;
import com.lxj.easyadapter.ViewHolder;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.widget.VerticalRecyclerView;
import com.ty.zls.R;
import com.ty.zls.activity.PrepayActivity;
import com.ty.zls.activity.WaterActivity;
import com.ty.zls.activity.WaterRecordActivity;
import com.ty.zls.bean.Account;

import java.util.List;


public class MyPopUp extends BottomPopupView {
    VerticalRecyclerView recyclerView;
    List<Account> accounts;
    Context context;
    int flag;

    public MyPopUp(@NonNull Context context, List<Account> accounts, int flag) {
        super(context);
        this.accounts = accounts;
        this.context = context;
        this.flag = flag;
    }
    @Override
    protected int getImplLayoutId() {
        return R.layout.custom_bottom_popup;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        recyclerView = findViewById(R.id.recyclerView);
        final CommonAdapter<Account> commonAdapter = new CommonAdapter<Account>(R.layout.adapter_query, accounts) {
            @Override
            protected void convert(@NonNull ViewHolder holder, @NonNull Account s, int position) {
                TextView view = holder.getView(R.id.tv_result_id);
                view.setText(String.valueOf(s.getId()));
                view = holder.getView(R.id.tv_res_address);
                view.setText(s.getArea());
                view = holder.getView(R.id.tv_res_name);
                view.setText(s.getName());
            }
        };
        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.SimpleOnItemClickListener(){
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, final int position) {
                dismissWith(new Runnable() {
                    @Override
                    public void run() {
                        Account account = accounts.get(position);
                        if(flag == 0){
                            //票据重打
                            Intent intent = new Intent(context,WaterRecordActivity.class);
                            intent.putExtra("account",account);
                            context.startActivity(intent);
                        } else if(flag == 1){
                            Intent intent = new Intent(context, WaterActivity.class);
                            intent.putExtra("id",account.getId());
                            context.startActivity(intent);
                        } else if(flag == 2){
                            Intent intent = new Intent(context, PrepayActivity.class);
                            intent.putExtra("account",account);
                            context.startActivity(intent);
                        }
                    }
                });
            }
        });
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(commonAdapter);
    }


}